#encoding:utf8 

import os, json, time, logging 
import tornado.websocket


#======================================================================
#------------------cs
'''
    msg = {
        act = ''
        base = {
            mac = todo,         --mac address 
            ip = todo,          --ip 
        }
        cs = {
            val = if exits! || 0     --control txt         
        }
    }
'''

#------------------sc
'''
    msg = {
        act = ''
        base = {
            mac = todo,         --mac address 
            ip = todo,          --ip 
        }
        sc = {
            r = 0
        }
    }
'''
#======================================================================

class piHandler(tornado.websocket.WebSocketHandler):
    _clis = set() 

    _mac_white = set() 
    _ip_white = set() 

    _mac_black = set() 
    _ip_black = set() 

    def initialize(self):
        #todo init list or 'white' and 'black'
        pass

    def on_message(self , message):

        message = json.loads(message)

        #todo chk base!           
        act = message["act"]
        argv= message["cs"]
        
        logicret = ""
        try:
            logicret = self.__getattribute__(act)(argv)
        except:
            logicret = "1"

        #todo ret creater!
        #template ret 
        ret = dict(
            act = "light", 
            base = dict(
                mac = "",
                ip = "1"
            ),
            sc = logicret
        )


        self.write_message(json.dumps(ret))

    def light(self, retval):
        
        #todo act logic

        #template return
        ret = {"r":0}
        return ret


